﻿
Partial Class WebForm_ManageEcolife
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Me.InitData()

            Dim sql As New MyStringBuilder
            Dim dat As New clsData
            sql.Length = 0
            sql.AppendFormat("Select EmployeeID,EmployeeName from Employee ")
            sql.AppendFormat("where EmployeeID = '{0}' ", clsAuth.GetUserID())
            sql.AppendFormat("and OrganizationID in ('2','14') ")

            If dat.GetData(sql).Rows.Count > 0 Then
                Me.ExportEcolifeDataTabPanel.Visible = True
            End If
        End If
    End Sub

    Public Sub InitData()
        Me.AddCleanPlan1.InitData(0, 0)
        Me.AddDirtyHeader1.InitData(0, 0)
        'Me.plCleanResult.Visible = False
    End Sub

#Region "Ecolife帳號查詢"
    Protected Sub SearchEcolifeID1_Loaded(ByVal source As Object, ByVal e As System.EventArgs) Handles SearchEcolifeID1.Loaded
        Me.SearchEcolifeID1.BindGV()
    End Sub
#End Region

#Region "Ecolife日誌查詢"
    Protected Sub SearchEcolifeID1_SelectedIndexChanged(ByVal source As Object, ByVal e As System.EventArgs) Handles SearchEcolifeID1.SelectedIndexChanged
        Me.TabContainer1.ActiveTab = Me.SearchEcolifeDataTabPanel
        Me.SearchEcolifeData1.EcolifeID = Me.SearchEcolifeID1.EcolifeID
        Me.SearchEcolifeData1.BindGV()
    End Sub
#End Region

#Region "執行服務計畫"
    '新增"執行"服務計畫
    Protected Sub SearchEcolifeData1_AddCleanPlan(ByVal sender As Object, ByVal e As System.EventArgs) Handles SearchEcolifeData1.AddCleanPlan
        Me.ImportEcolifeDataTabPanel.Enabled = True
        Me.TabContainer1.ActiveTab = Me.ImportEcolifeDataTabPanel
        Me.MultiView1.SetActiveView(vwAddCleanPlan)

        '新增模式
        Me.AddCleanPlan1.DataAccessMode = FormViewMode.Insert
        Me.AddCleanPlan1.InitData(Me.SearchEcolifeData1.SelectedPlanID, 0)
        Dim MyWorkHeaderID As String = Me.AddCleanPlan1.EcolifeInsert(Me.SearchEcolifeData1.CaseNo)
        Me.AddCleanPlan1.DataAccessMode = FormDataAccessMode.Execute
        Me.AddCleanPlan1.InitData(Me.SearchEcolifeData1.SelectedPlanID, MyWorkHeaderID)
    End Sub

    '在"執行"服務計畫頁按下存檔
    Protected Sub AddCleanPlan1_Save_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddCleanPlan1.Save_Click
        Me.Save_Finish()
    End Sub
#End Region

#Region "執行髒亂點通報"
    '新增"執行"髒亂點通報
    Protected Sub SearchEcolifeData1_AddDirtyHeader(ByVal sender As Object, ByVal e As System.EventArgs) Handles SearchEcolifeData1.AddDirtyHeader
        Me.ImportEcolifeDataTabPanel.Enabled = True
        Me.TabContainer1.ActiveTab = Me.ImportEcolifeDataTabPanel
        Me.MultiView1.SetActiveView(vwAddDirtyHeader)

        'MyObj.WriteLog("AddDirtyHeader")
        '新增模式
        Me.AddDirtyHeader1.DataAccessMode = FormViewMode.Insert
        Me.AddDirtyHeader1.InitData(Me.SearchEcolifeData1.SelectedPlanID, 0)
        Dim MyWorkHeaderID As String = Me.AddDirtyHeader1.EcolifeInsert(Me.SearchEcolifeData1.CaseNo)
        Me.AddDirtyHeader1.DataAccessMode = FormDataAccessMode.Execute
        Me.AddDirtyHeader1.InitData(Me.SearchEcolifeData1.SelectedPlanID, MyWorkHeaderID)
    End Sub

    '在"執行"髒亂點通報頁按下存檔
    Protected Sub AddDirtyHeader1_Save_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddDirtyHeader1.Save_Click
        Me.Save_Finish()
    End Sub
#End Region

#Region "新增髒亂點通報"
    '新增髒亂點通報
    Protected Sub SearchEcolifeData1_AddWorkHeader(ByVal source As Object, ByVal e As System.EventArgs) Handles SearchEcolifeData1.AddWorkHeader
        Me.ImportEcolifeDataTabPanel.Enabled = True
        Me.TabContainer1.ActiveTab = Me.ImportEcolifeDataTabPanel
        Me.MultiView1.SetActiveView(vwAddWorkHeader)

        'MyObj.WriteLog("AddWorkHeader")
        '新增模式
        Me.AddWorkHeader1.DataAccessMode = My.DataAccessMode.Insert
        Me.AddWorkHeader1.SetData(Me.SearchEcolifeData1.CaseNo)
    End Sub

    '在新增髒亂點通報按下存檔
    Protected Sub AddWorkHeader1_Saving(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddWorkHeader1.Saving
        If Me.SaveData = True Then
            Me.Save_Finish()
        End If
    End Sub

    Private Function SaveData() As Boolean
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        Using scope As New Transactions.TransactionScope
            If (Me.AddWorkHeader1.DataAccessMode = My.DataAccessMode.Insert) Then
                sql.Length = 0
                sql.AppendFormat("insert FindDirtyHeader (EcolifeCaseNo,RegionType,VillageID,Location,Description,Status,ReportDate,PhotoPathID,CreateDate,CreateBy) select ")
                sql.AppendFormat(" '{0}' ", Me.SearchEcolifeData1.CaseNo)
                sql.AppendFormat(",'{0}' ", Me.AddWorkHeader1.RegionType)
                sql.AppendFormat(",'{0}' ", Me.AddWorkHeader1.VillageID)
                sql.AppendFormat(",'{0}' ", Me.AddWorkHeader1.Location)
                sql.AppendFormat(",'{0}' ", Me.AddWorkHeader1.Description)
                sql.AppendFormat(",'{0}' ", "1")
                sql.AppendFormat(",'{0:yyyy/MM/dd}' ", Me.AddWorkHeader1.ReportDate)
                sql.AppendFormat(",'{0}' ", Me.AddWorkHeader1.PhotoPathID)
                sql.AppendFormat(",{0} ", "getdate()")
                sql.AppendFormat(",'{0}' ", Me.Context.User.Identity.Name)
                dat.AccData(sql)
                Dim id As Integer = MyObj.GetIdentity()

                Dim s1 As String = Me.AddWorkHeader1.DirtyItem(0, 0)
                Dim s2 As String = Me.AddWorkHeader1.DirtyItem(0, 1)
                Dim s3 As String = Me.AddWorkHeader1.DirtyItem(0, 2)
                Dim s4 As String = Me.AddWorkHeader1.DirtyItem(0, 3)

                For i As Integer = 0 To (Me.AddWorkHeader1.DirtyItem.Length / 4) - 1
                    If Me.AddWorkHeader1.DirtyItem(i, 3) <> "" Then
                        sql.Length = 0
                        sql.AppendFormat("insert FindDirtyItem (FindDirtyHeaderID,ItemGroupID,ItemID,Amount,DirtyLevel) select ")
                        sql.AppendFormat(" '{0}' ", id)
                        sql.AppendFormat(" ,'{0}' ", Me.AddWorkHeader1.DirtyItem(i, 0))
                        sql.AppendFormat(" ,'{0}' ", Me.AddWorkHeader1.DirtyItem(i, 1))
                        sql.AppendFormat(" ,'{0}' ", Me.AddWorkHeader1.DirtyItem(i, 2))
                        sql.AppendFormat(" ,'{0}' ", Me.AddWorkHeader1.DirtyItem(i, 3))
                        dat.AccData(sql)
                    End If
                Next
                scope.Complete()
            End If
        End Using

        Return True
    End Function

#End Region

    Public Sub Save_Finish()
        Me.TabContainer1.ActiveTab = Me.SearchEcolifeDataTabPanel
        Me.UpdateCase(Me.SearchEcolifeData1.CaseNo)
        Me.SearchEcolifeData1.BindGV()
        Me.MultiView1.SetActiveView(vwNothing)
        Me.ImportEcolifeDataTabPanel.Enabled = False
        'MyObj.ShowMessage(Me.GetLocalResourceObject("SaveData_Success"))
    End Sub

    '將資料變更為已匯入
    Public Sub UpdateCase(ByVal CaseNo As String)
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.AppendFormat(" Update EcolifeData ")
        sql.AppendFormat(" set Active='{0}' ", "N")
        sql.AppendFormat(" ,ImportBy='{0}' ", Page.User.Identity.Name)
        sql.AppendFormat(" ,ImportDate=getdate() ")
        sql.AppendFormat(" where CaseNo='{0}' ", CaseNo)

        dat.AccData(sql.ToString())
    End Sub
End Class
